  //
//  myView.m
//  QuickReadComic
//
//  Created by 苗钧豪 on 16/9/1.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "myView.h"
#import "ComicTableViewCell.h"
#import "ZWYNetTool.h"
#import "ComicModel.h"
#import "TopicModel.h"
#import "UserModels.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "ComicViewController.h"
#import "webViewController.h"
@implementation myView
// 初始化方法
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.timeArr = [NSArray arrayWithObjects:@"1474387200",@"1474473600",@"1474560000",@"1474646400",@"1474732800",@"1474819200",@"0", nil];
        [self NetWork];
        // 布局
        self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) style:(UITableViewStylePlain)];
        // 设置代理人
        _tableview.delegate = self;
        _tableview.dataSource = self;
        // 注册
        UINib *nib = [UINib nibWithNibName:@"ComicTableViewCell" bundle:nil];
        [_tableview registerNib:nib forCellReuseIdentifier:@"comic"];
        // 添加到视图上
        [self addSubview:_tableview];
    }
    return self;
}
- (void)NetWork {
    for (int i = 0; i < 7; i ++) {
//        NSLog(@"^^^^^^^^^^^^^^^^^^^^^^^^^^^^%f",self.view.frame.origin.x);
        if (self.frame.origin.x / 375 == i) {
            [ZWYNetTool GET:[NSString stringWithFormat:@"http://api.kuaikanmanhua.com/v1/daily/comic_lists/%@",_timeArr[i]] andBody:@"since=0" andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
                NSLog(@"=====================成功啦");
                self.model = [comicDataModel modelWithDic:result[@"data"]];
                [self.tableview reloadData];
            } andFailureBlock:^(NSError *error) {
                NSLog(@"=====================出错啦");
            }];
        }
    }
}
// 自定义cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 400;
}
// 每个分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.model.comics.count;
}
// 设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    [self NetWork];
    ComicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"comic" forIndexPath:indexPath];
    ComicModel *comic = self.model.comics[indexPath.row];
    self.webUrl = comic.url;
    cell.typeLabel.text = comic.label_text;
    cell.titleLabel.text = comic.topic.title;
    cell.nameLabel.text = comic.topic.user.nickname;
    cell.numberLabel.text = comic.title;
    [cell.numberBtn setTitle:[NSString stringWithFormat:@"%@",comic.likes_count] forState:(UIControlStateNormal)];
    [cell.sayBtn setTitle:[NSString stringWithFormat:@"%@",comic.comments_count] forState:(UIControlStateNormal)];
    [cell.PhotoImageview sd_setImageWithURL:[NSURL URLWithString:comic.cover_image_url]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"111111111111111111111");
    ComicModel *comic = self.model.comics[indexPath.row];
    self.webUrl = comic.url;
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.webUrl]];
    webViewController *wvc = [[webViewController alloc]init];
    wvc.urlStr = comic.url;
    UINavigationController *navc = [[UINavigationController alloc]initWithRootViewController:wvc];
    [UIApplication sharedApplication].keyWindow.rootViewController = navc;
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
